﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace AdminModule.CommonClasses
{
    public enum BusyEvent { LoadData, Save, Copy };
    public enum FreeEvent { LoadData, Save, Copy };

    public class EventsContainer
    {
        public delegate void BusyHandler(BusyEvent type);
        public event BusyHandler Busy;
        public delegate void FreeHandler(FreeEvent type);
        public event FreeHandler Free;

        public delegate void DataLoadCompletedHandler();
        public delegate void SaveCompletedHandler(object sender, AsyncCompletedEventArgs e);
        public delegate void CopyCompletedHandler(object sender, AsyncCompletedEventArgs e);

        public delegate void DataLoadStartedHandler();
        public delegate void SaveStartedHandler();
        public delegate void CopyStartedHandler();

        public event DataLoadCompletedHandler DataLoadCompleted;
        public event SaveCompletedHandler SaveCompleted;
        public event CopyCompletedHandler CopyCompleted;

        public event DataLoadStartedHandler DataLoadStarted;
        public event SaveStartedHandler SaveStarted;
        public event CopyStartedHandler CopyStarted;

        public void RaiseBusyEvent(BusyEvent type)
        {
            switch (type)
            {
                case BusyEvent.LoadData:
                    this.RaiseDataLoadStarted();
                    break;

                case BusyEvent.Save:
                    this.RaiseSaveStarted();
                    break;
                
                case BusyEvent.Copy:
                    this.RaiseCopyStarted();
                    break;
            }

            this.RaiseBusy(type);
        }

        public void RaiseFreeEvent(FreeEvent type, object sender = null, AsyncCompletedEventArgs e = null)
        {
            switch (type)
            {
                case FreeEvent.LoadData:
                    this.RaiseDataLoadCompleted();
                    break;

                case FreeEvent.Save:
                    this.RaiseSaveCompleted(sender, e);
                    break;

                case FreeEvent.Copy:
                    this.RaiseCopyCompleted(sender, e);
                    break;
            }

            this.RaiseFree(type);
        }

        #region Занят / свободен
        private void RaiseBusy(BusyEvent type)
        {
            if (Busy != null)
            {
                Busy(type);
            }
        }

        private void RaiseFree(FreeEvent type)
        {
            if (Free != null)
            {
                Free(type);
            }
        }
        #endregion

        #region Выполнено
        private void RaiseDataLoadCompleted()
        {
            if (DataLoadCompleted != null)
            {
                DataLoadCompleted();
            }
        }

        private void RaiseSaveCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (SaveCompleted != null)
            {
                SaveCompleted(sender, e);
            }
        }

        private void RaiseCopyCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (CopyCompleted != null)
            {
                CopyCompleted(sender, e);
            }
        }
        #endregion

        #region Запущено
        private void RaiseDataLoadStarted()
        {
            if (DataLoadStarted != null)
            {
                DataLoadStarted();
            }
        }

        private void RaiseSaveStarted()
        {
            if (SaveStarted != null)
            {
                SaveStarted();
            }
        }

        private void RaiseCopyStarted()
        {
            if (CopyStarted != null)
            {
                CopyStarted();
            }
        }
        #endregion
    }
}
